<?php
//快钱支付
class _99bill extends Pay {

    var $payurl = 'https://www.99bill.com/webapp/receiveMerchantInfoAction.do?';
    var $scode  = 'xxxxxxxxxxxxxxxxxxxxx'; //密钥
    var $c_mid  = '111111111111111111111'; //商户编号

    var $payresult = 'http://www.loopo.cn/shell/payresult.php/3'; //返回地址

    function _payto() {

        //折扣
        $this->data['sumfee'] += $this->data['adjustfee'];

        //标题
        $subject    = '【乐铺'.$this->id.'号订单】';
        foreach ($this->items as $v) {
            $amount += $v['amount'];
            $body .= "\n{$v['iname']} x {$v['amount']}\n";
        }

        $body = $subject."\n".$body;
        $linkman = $this->data['cman'] ? $this->data['cman'] : $this->data['linkman'];

        //c_signstr =“merchant_id=merchant_id 值&orderid=orderid 值&amount=amount 值&merchant_url=merchant_url值&merchant_key= key值”
        $md5str = "merchant_id={$this->c_mid}&orderid={$this->id}&amount=". ($this->data['sumfee'] + $this->data['postfee']) . "&merchant_url={$this->payresult}&merchant_key={$this->scode}";
        $md5    = strtoupper(md5(trim($md5str)));

        //urlencode
        $baseurl = "merchant_id={$this->c_mid}&orderid={$this->id}&amount=". ($this->data['sumfee'] + $this->data['postfee']) ."&currency=1&merchant_url={$this->payresult}&pname=". urlencode($linkman) ."&commodity_info=". urlencode($body) ."&pemail=&isSupportDES=2&mac=$md5";
        //header("location:".$this->payurl.$baseurl);
        return $this->payurl.$baseurl;
    }

    /*
    1.成功
    2.失败
    */
    function _payresult() {

        //print_r($this->request);
        //http://www.loopo.cn/shell/payresult.php/3?merchant_id=商户编号&orderid=订单编号&amount=金额&date=YYYYMMDD&merchant_param=附加信息&succeed=Y/N&mac=$md5str

        $this->id = $this->request['orderid'];
        $result['code']['payResult'] = 2;
        $result['data'] = $this->request;

        //md5=“merchant_id=merchant_id 值&orderid=orderid 值&amount=amount 值& date = date 值&succeed=succeed值&merchant_key= key值”
        if (isset($this->request['merchant_id']) && isset($this->request['orderid']) && isset($this->request['amount']) && isset($this->request['date']) && isset($this->request['succeed']) && isset($this->request['mac'])) {
            $md5str = "merchant_id={$this->request['merchant_id']}&orderid={$this->request['orderid']}&amount={$this->request['amount']}&date={$this->request['date']}&succeed={$this->request['succeed']}&merchant_key={$this->scode}";

            if (strtoupper(md5($md5str)) == $this->request['mac']) {
                $result['code']['payResult'] = 1;

                //实付金额
                if (trim($this->request['succeed']) == 'Y') $result['pay'] = (float)$this->request['amount'];
            }
        }

        return $result;
    }
}
?>
